package proxies.events
{
	
	import flash.events.EventDispatcher;

	public class ProxyEventsDispatcher implements IProxyEventsDispatcher
	{
		private static const USE_WEAK_REFERENCES:Boolean = true;
		
		private static var instance:ProxyEventsDispatcher;
		private static var allowInstance:Boolean = false;			
		
		private var eventDispatcher:EventDispatcher = new EventDispatcher();
		
		/**
		 * This class implement the Singleton pattern so direct instance is not allowed.
		 * <br />
		 * To get an instance, use getInstance() method
		 * 
		 * @see getInstance
		 */		
		public function ProxyEventsDispatcher() {
			if(!allowInstance)
				throw new Error('Multiple instance not allowed in ProxyEventsDispatcher class !');			
		}		
		
		/**
		 * Get the unique instance of this class
		 * 
		 * @return the unique instance
		 */		
		public static function getInstance():ProxyEventsDispatcher {
			if(instance == null) {
				allowInstance = true;
				instance = new ProxyEventsDispatcher();
				allowInstance = false;
			}
			
			return instance;
		}
		
		public function addEventListener(eventType:String, callbackFunction:Function):void
		{
			this.eventDispatcher.addEventListener(eventType, callbackFunction, false, 0, USE_WEAK_REFERENCES);
		}
		
		public function removeEventListener(eventType:String, callbackFunction:Function):void
		{
			this.eventDispatcher.addEventListener(eventType, callbackFunction, false, 0, USE_WEAK_REFERENCES);
		}
		
		public function dispatchEvent(event:ProxyEvent):void
		{
			this.eventDispatcher.dispatchEvent(event);
		}
	}
}